#include "StdAfx.h"
#include "CircuitJunctionDisObj.h"


CCircuitJunctionDisObj::CCircuitJunctionDisObj(CCircuitJunction *pJunction)
	: CCircuitUnitDisObj((CCircuitUnit *)pJunction)
{
	m_dblColor[0] = pJunction->GetColorR();
	m_dblColor[1] = pJunction->GetColorG();
	m_dblColor[2] = pJunction->GetColorB();
}

CCircuitJunctionDisObj::CCircuitJunctionDisObj(const CCircuitJunctionDisObj *pDisObj, CCircuitJunction *pJunction/* =NULL */)
	: CCircuitUnitDisObj(pDisObj, (CCircuitUnit *)pJunction)
{

}

CCircuitJunctionDisObj::~CCircuitJunctionDisObj(void)
{
}

void CCircuitJunctionDisObj::Serialize(CArchive & ar)
{
	CCircuitUnitDisObj::Serialize(ar);
}

void CCircuitJunctionDisObj::Display(CHoopsMap *pHoopsMap)
{
	CCircuitJunction *pCirJunction = (CCircuitJunction *)GetEntityObj();
	if (!pCirJunction)
	{
		return;
	}

	char sCirJuncSeg[64];
	sprintf(sCirJuncSeg, "junction_%d", pCirJunction->GetId());
	HC_KEY key_obj = HC_Open_Segment(sCirJuncSeg);
		HC_Flush_Contents(".", "everything");

		//HC_Insert_Marker(pCirJunction->GetPosX(), pCirJunction->GetPosY(), 0.0f);
		//HC_Set_Marker_Size(pCirJunction->GetRadius());
		HPoint center, normal;
		center.x=pCirJunction->GetPosX(); center.y=pCirJunction->GetPosY(); center.z=-1.0f;
		normal.x=0; normal.y=0; normal.z=1;
		HC_Insert_Circle_By_Radius(&center, pCirJunction->GetRadius()*1.5, &normal);

		char szColor[64];
		sprintf(szColor, "faces = edges = (r=%.2f g=%.2f b=%.2f)", 
			GetColorR(), GetColorG(), GetColorB());
		HC_Set_Color(szColor);

		//HC_Set_Visibility("faces=on, edges=off");
	HC_Close_Segment();

	pHoopsMap->Delete(this);
	pHoopsMap->Add(key_obj, this);
}

void CCircuitJunctionDisObj::Transform(const CMatrix transf)
{

}

CObj *CCircuitJunctionDisObj::Clone() const
{
	return new CCircuitJunctionDisObj(this);
}

bool CCircuitJunctionDisObj::GetBox(double & dblLeft, double & dblBottom, double & dblRight, double & dblTop) const
{
	return false;
}

eCircuitUnitType CCircuitJunctionDisObj::GetCircuitUnitType() const
{
	return CIRCUIT_JUNCTION_DISOBJ;
}

CPos CCircuitJunctionDisObj::GetConnectPos()
{
	CCircuitJunction *pJunction = (CCircuitJunction *)GetEntityObj();
	ASSERT (pJunction);

	CICircuit *pICirc = (CICircuit *)pJunction->GetParentEntity();
	ASSERT (pICirc);

	CICircuitDisObj *pICircDis = (CICircuitDisObj *)pICirc->GetDisplayObjByLayerID(m_lLayerId);
	ASSERT(pICircDis);

	CMatrix matrix = pICircDis->GetTransform();
	CMatrix operMatrix = pICircDis->GetOperTransf(); 
	matrix = operMatrix.CrossProduct(matrix);

	float data[16];
	matrix.Get3Dtransf(data);

	CTransform transform;
	transform.set(data);

	CPos posTmp(pJunction->GetPosX(), pJunction->GetPosY(), 0.0);
	CPos posNew = transform.TransformPos(posTmp);

	return posNew;
}